<template>
  <div class="login-con">
    <h3>登录系统</h3>
    <div class="login-center">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item prop="uname">
          <el-input
            v-model="ruleForm.uname"
            prefix-icon="iconfont icon-yonghu"
          ></el-input>
        </el-form-item>
        <el-form-item prop="pass">
          <el-input
            :type="passFlag ? 'password' : 'text'"
            v-model="ruleForm.pass"
            prefix-icon="iconfont icon-mima"
            autocomplete="off"
          >
            <template #suffix>
              <i
                class="iconfont iconpass"
                :class="passFlag ? 'icon-eyes' : 'icon-eyes-'"
                @click="passFlag = !passFlag"
              ></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user.js'
import local from '@/utils/local.js'
export default {
  data() {
    var checkUname = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        pass: '',
        uname: ''
      },
      rules: {
        pass: [{ validator: validatePass, trigger: 'blur' }],
        uname: [{ validator: checkUname, trigger: 'blur' }]
      },
      passFlag: true
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const { code, msg, token } = await login({
            account: this.ruleForm.uname,
            password: this.ruleForm.pass
          })
          if (code === 0) {
            this.$message({
              message: msg,
              type: 'success'
            })
            setTimeout(() => {
              this.$router.push('/')
            }, 1500)
            local.set('token', token)
          } else {
            this.$message.error(msg)
          }
        } else {
          this.$message.error('填点东西吧,求求了')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-con {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #2d3a4b;

  h3 {
    color: #fff;
    text-align: center;
    letter-spacing: 3px;
    font-weight: 400;
    margin-bottom: 30px;
    user-select: none;
  }

  .login-center {
    width: 380px;
  }

  .el-input {
    user-select: none;
  }

  .iconpass {
    cursor: pointer;
  }

  .el-button {
    width: 100%;
    letter-spacing: 5px;
  }
}
</style>
